//
//  UIButton+Vertical.m
//  JimiPay
//
//  Created by yull on 16/1/7.
//  Copyright © 2016年 com.shengyun. All rights reserved.
//

#import "UIButton+Util.h"

@implementation UIButton (Util)

- (void)centerImageAndTitle:(float)spacing
{
    // get the size of the elements here for readability
    
    CGSize imageSize = self.imageView.frame.size;
    CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(1000, self.frame.size.height)];
    
    // get the height they will take up as a unit
    CGFloat totalHeight = (imageSize.height + titleSize.height + spacing);
    
    // raise the image and push it right to center it
    self.imageEdgeInsets = UIEdgeInsetsMake(
                                            - (totalHeight - imageSize.height), 0.0, 0.0, - titleSize.width);
    
    // lower the text and push it left to center it
    self.titleEdgeInsets = UIEdgeInsetsMake(
                                            0.0, - imageSize.width, - (totalHeight - titleSize.height), 0.0);
}

- (void)centerImageAndTitle
{
    const int DEFAULT_SPACING = 6.0f;
    [self centerImageAndTitle:DEFAULT_SPACING];
}

@end
